<template>
  <div class="chat">
    <div class="chat-box">
      <div class="chat-info">
        <div class="info-detail" v-for="(item, k) in msgList" :key="k">
          <div :class="item.left ? 'head' : 'head-a'">
            <span class="name">{{ item.name }}</span>
            <span>{{ item.date }}</span>
          </div>
          <div :class="item.left ? 'info-main' : 'info-main-a'">
            <span>{{ item.msg }}</span>
          </div>
        </div>
      </div>
      <div class="chat-message">
        <el-input
          v-model="msg"
          placeholder="请输入内容"
          @keyup.enter.native="sendMessage"
        >
          <el-button slot="append" type="primary" @click="sendMessage"
            >发送</el-button
          >
        </el-input>
      </div>
    </div>
  </div>
</template>

<script>
const ws = new WebSocket('ws://localhost:18888/socketTest')
export default {
  data() {
    return {
      msg: '',
      msgList: []
    }
  },
  mounted() {
    //   绑定webSocket事件
    ws.addEventListener('open', this.handleWsOpen.bind(this), false)
    ws.addEventListener('close', this.handleWsClose.bind(this), false)
    ws.addEventListener('error', this.handleWsError.bind(this), false)
    ws.addEventListener('message', this.handleWsMessage.bind(this), false)
  },
  methods: {
    sendMessage() {
      const currentY = new Date().getFullYear()
      let currentM = new Date().getMonth() + 1
      let currentD = new Date().getDate()
      let currentH = new Date().getHours()
      let currentMin = new Date().getMinutes()
      let currentS = new Date().getSeconds()
      if (currentM < 10) {
        currentM = `0${currentM}`
      }
      if (currentD < 10) {
        currentD = `0${currentD}`
      }
      if (currentH < 10) {
        currentH = `0${currentH}`
      }
      if (currentMin < 10) {
        currentMin = `0${currentMin}`
      }
      if (currentS < 10) {
        currentS = `0${currentS}`
      }
      const date = `${currentY}-${currentM}-${currentD} ${currentH}:${currentMin}:${currentS}`
      const name = window.sessionStorage.getItem('nickname')
      const msg = JSON.stringify({
        msg: this.msg,
        name: name,
        date: date,
        left: true
      })
      ws.send(msg)
      // 发送完清空聊天框
      this.msg = ''
    },
    handleWsOpen() {
      console.log('聊天已经启动！')
    },
    handleWsClose() {
      console.log('聊天已经关闭！')
    },
    handleWsError() {
      console.log('聊天出错！')
    },
    // 收发消息
    handleWsMessage(e) {
      console.log(e)
      const msg = JSON.parse(e.data)
      this.msgList.push(msg)
    }
  }
}
</script>

<style lang="less" scoped>
.chat {
  padding: 100px 300px 0 300px;
  display: flex;
  justify-content: center;
  .chat-box {
    margin-top: 20px;
    padding: 10px 10px 10px 10px;
    width: 400px;
    height: 600px;
    background-color: #fff;
    border-radius: 10px 10px 0 0;
    .chat-info {
      height: 550px;
      overflow: auto;
      .info-detail {
        margin-bottom: 5px;
        .head {
          width: 100%;
          margin-bottom: 5px;
          display: flex;
          flex-direction: row-reverse;
          .name {
            margin-left: 5px;
            color: rgb(240, 74, 74);
          }
        }
        .head-a {
          width: 100%;
          margin-bottom: 5px;
          .name {
            margin-right: 5px;
            color: rgb(22, 189, 240);
          }
        }
        .info-main-a {
          font-size: 18px;
          font-family: 楷体;
        }
        .info-main {
          font-size: 18px;
          font-family: 楷体;
          display: flex;
          flex-direction: row-reverse;
        }
      }
    }
    .chat-message {
      height: 50px;
    }
  }
}
</style>
